<?php
class MessageController extends Controller {
	protected $conversation;
	private $topSalle;
	private $topMessage;
	
	public function __construct() {
		$this->topSalle = Loader::loadModel('Salle');
		$this->topSalle = Salle::getTopSalles();
		$this->topMessage = Loader::loadModel('Message');
		$this->topMessage = Message::get10MostLikedMessages();
		
	}
	
	public function view($page='restrictedPage',$params=null) {
		$message = Loader::loadModel('MessagePrives');
		$args = ParameterManager::getInstance()->getParam('args');
		
		$userController = Loader::loadController('user');
		if($userController->isConnected()) {
			$session_user = $userController->getUserSessionInfos();
			$connectedUsers = Loader::loadModel('User');
			$connectedUsers = User::getConnectedUsers();
			$menu = 'topUserLogged';
			if($userController->getUserSessionInfos()->getStatusId()!=1)
				$menu = 'topUserLogged';
			else
				$menu = 'topAdmin';
			
			if(is_array($args) && isset($args[0])) {
				$message = MessagePrives::getMessages($session_user->getId(),$args[0]);
				MessagePrives::readMessagesBetween($session_user->getId(), $args[0]);
				$user = Loader::loadModel('User');
				$user = User::getUserInfos($args[0]);
				
					
				if(!empty($message)) {
					$view = Loader::loadView('View', array('top'=>$menu,'left'=>'leftColumn','center'=>'conversation'));
					$view->setParam('other_user',$user);
					$view->setParam('message',$message);
					
				} else {
					$view = Loader::loadView('View', array('top'=>$menu,'left'=>'leftColumn','center'=>'errorMessage'));
					$view->setParam('message',$message);
				}
				
				$isPost = ParameterManager::getInstance()->getPost('POSTMP');
				if(isset($isPost)) {
					$message = ParameterManager::getInstance()->getPost('MESSAGEPRIVE');
					Loader::loadModel('MessagePrives');
					MessagePrives::createMessagePrive($session_user->getId(), $user->getId(), $message);
				}
			} else {
				$isOnDeleteAction = ParameterManager::getInstance()->getPost('DELETEACTION');
				if($isOnDeleteAction) {
					$other_id = ParameterManager::getInstance()->getPost('OTHERID');
					Loader::loadModel('MessagePrives');
					MessagePrives::deleteConversationBetween($session_user->getId(), $other_id);
				}
				$view = Loader::loadView('View', array('top'=>$menu,'left'=>'leftColumn','center'=>'messagesPrives'));
				$message = Loader::loadModel('MessagePrives');
				$message = MessagePrives::getConversations($session_user->getId());
				$view->setParam('messagesprives', $message);
			}
			$view->setParam('user',$session_user);
			$view->setParam('topsalles',$this->topSalle);
			$view->setParam('topmessages',$this->topMessage);
			$view->setParam('connectedUsers', $connectedUsers);
			$view->render();
		} else {
				$view = Loader::loadView('View', array('top'=>'topUserNotLogged','left'=>'leftColumnNotLogged','center'=>'restrictedPage'));
				$view->setParam('topsalles',$this->topSalle);
				$view->setParam('topmessages',$this->topMessage);
				$view->render();
		}
	}
	
	public function create() {
		$userController = Loader::loadController('user');
		if($userController->isConnected()) {
			$session_user = $userController->getUserSessionInfos();
			$connectedUsers = Loader::loadModel('User');
			$connectedUsers = User::getConnectedUsers();
			
			$isPost = ParameterManager::getInstance()->getPost('POSTMESSAGE');
			if(isset($isPost)) {
				$destinataire = ParameterManager::getInstance()->getPost('USER');
				$contenu = ParameterManager::getInstance()->getPost('CONTENU');
				Loader::loadModel('MessagePrives');
				MessagePrives::createMessagePrive($userController->getUserSessionInfos()->getId(), $destinataire[0], $contenu);
			}
			
			$allUsers = User::getAllUsers($userController->getUserSessionInfos()->getId());
			if(isset($isPost))
				$this->view();
			else {
				if($userController->getUserSessionInfos()->getStatusId()!=1)
					$view = Loader::loadView('View', 
											array('title'=>'Créer un message','top'=>'topUserLogged','left'=>'leftColumn','center'=>'createMessage'));
				else 
					$view = Loader::loadView('View', 
											array('title'=>'Créer un message','top'=>'topAdmin','left'=>'leftColumn','center'=>'createMessage'));
				$view->setParam('user',$session_user);
				$view->setParam('allUsers', $allUsers);
				
				$view->setParam('topsalles',$this->topSalle);
				$view->setParam('topmessages',$this->topMessage);
				$view->setParam('connectedUsers', $connectedUsers);
				$view->render();
			}
		} else {
				$view = Loader::loadView('View', array('top'=>'topUserNotLogged','left'=>'leftColumnNotLogged','center'=>'restrictedPage'));
				$view->setParam('topsalles',$this->topSalle);
				$view->setParam('topmessages',$this->topMessage);
				$view->render();
		}
	}
	
	public function defaut() {
		$this->view();
	}
	
}